package com.usian.mongodb;

import com.usian.mongodb.entity.User;
import com.usian.mongodb.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.*;

import java.util.Date;
import java.util.List;

@SpringBootTest
class MongodbApplicationTests2 {

    @Autowired
    private UserRepository userRepository;

    /**
     * 添加
     */
    @Test
    void insert() {
        for (int i = 0; i < 10; i++) {
        User user = new User();
        user.setAge(18);
        user.setName("李四_" + i);
        user.setCreateDate(new Date()+"");
        user.setEmail("abc@qq.com");
        User insert = userRepository.insert(user);
        System.out.println(insert);
        }
    }

    /**
     * 查询所有
     */
    @Test
    void findAll(){
        System.out.println(userRepository.findAll());
    }


    /**
     * 根据id查询
     */
    @Test
    void findById(){
        System.out.println(userRepository.findById("63b6e42c6e50a013aeaae2fe"));
    }


    /**
     * 条件查询
     */
    @Test
    void findBy(){
        User user = new User();
        user.setAge(18);
        user.setName("李四_1");
        System.out.println(userRepository.findAll(Example.of(user)));
    }
    /**
     * 模糊查询
     */
    @Test
    void findLike(){
//创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("三");
        Example<User> userExample = Example.of(user, matcher);
        List<User> userList = userRepository.findAll(userExample);
        System.out.println(userList);
    }

    /**
     * 分页查询
     */
    @Test
    void findLimit(){
        Sort sort = Sort.by(Sort.Direction.DESC, "age");
//0为第一页
        Pageable pageable = PageRequest.of(0, 10, sort);
//创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("三");
        Example<User> userExample = Example.of(user, matcher);
//创建实例
        Example<User> example = Example.of(user, matcher);
        Page<User> pages = userRepository.findAll(example, pageable);
        System.out.println(pages);
    }
    /**
     * 修改
     */
    @Test
    void update(){
        User user = userRepository.findById("63b6e42c6e50a013aeaae2ff").get();
        user.setName("王五");
        user.setAge(25);
        user.setEmail("883220990@qq.com");
        User save = userRepository.save(user);
        System.out.println(save);
    }
    /**
     * 删除
     */
    @Test
    void delete(){
        userRepository.deleteById("63b6e42c6e50a013aeaae2fe");
    }
}
